#include "..\include_eapi_header.h"
#include<iphlpapi.h>
#include<memory>
#pragma comment(lib,"Iphlpapi.lib")

inline char* GetLocalMac(int NetIndex) {
	char* result = nullptr;
	ULONG size = 0;
	int index = 0;
	IP_ADAPTER_INFO* info;
	GetAdaptersInfo(nullptr, &size);
	if (size) {
		info = reinterpret_cast<IP_ADAPTER_INFO*>(new char[size]);
		if (GetAdaptersInfo(info, &size) == ERROR_SUCCESS) {
			IP_ADAPTER_INFO* current = info;
			while (current) {
				if (index == NetIndex - 1) {
					char Dest[40] = { 0 };
					sprintf_s(Dest, "%02X-%02X-%02X-%02X-%02X-%02X", current->Address[0], current->Address[1], current->Address[2], current->Address[3], current->Address[4], current->Address[5]);
					result = CloneTextData(Dest);
					break;
				}
				current = current->Next;
				++index;
			}
		}
		delete[] reinterpret_cast<char*>(info);
	}
	return result;
}

EAPI_EXTERN_C void eapi_GetLocalMac_44_eapi(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	int NetIndex = 1;
	if (pArgInf->m_dtDataType && pArgInf->m_int > 1)
	{
		NetIndex = pArgInf->m_int;
	}
	pRetData->m_pText = GetLocalMac(NetIndex);

}

